package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.BaseCategoryViewService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.search.AttributeValueIndex;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {

	@Autowired
	private AlbumInfoService albumInfoService;
	@Autowired
	public BaseCategoryViewService baseCategoryViewService;
   //--------------------------------------------------------------------------------------------------
   //添加和删除粗忽可以看成是逆操作，都需要至少以下四步													 ||
	//1.保存(或删除)专辑信息 2.保存(或删除)专辑属性值 3.保存(或删除)专辑属性值关联关系 4.保存(或删除)专辑统计数据||
   //------------------------------	-------------------------------------------------------------------

	//api/album/albumInfo/saveAlbumInfo
	/*因为是保存数据到数据库，所以使用post方法。而且要返回数据，所以使用Result封装。
	还有要传入参数，所以使用@RequestBody注解，所以只用在imp层完成业务逻辑，不用去查数据库，所以不用在imp层写接口。
	*/
	@GuiguLogin
	@Operation(summary = "保存专辑信息")
	@PostMapping("saveAlbumInfo")
	public Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo){
		albumInfoService.saveAlbumInfo(albumInfoVo);
		return Result.ok();
	}

	//api/album/albumInfo/findUserAlbumPage/1/10
	@GuiguLogin
	@Operation(summary = "分页查询用户专辑列表")
	@PostMapping("findUserAlbumPage/{pageNum}/{pageSize}")
	public Result findUserAlbumPage(
			@PathVariable("pageNum") Long pageNum,
			@PathVariable("pageSize") Long pageSize,
			@RequestBody AlbumInfoQuery  albumInfoQuery){
	Page<AlbumListVo> page = albumInfoService.findUserAlbumPage(pageNum,pageSize,albumInfoQuery);
	return Result.ok(page);
	}

	// api/album/albumInfo/removeAlbumInfo/1594
	@GuiguLogin
	@Operation(summary = "删除专辑")
	@DeleteMapping("removeAlbumInfo/{id}")
	public Result removeAlbumInfo(@PathVariable("id") Long id){
		albumInfoService.removeAlbumInfoById(id);
		return Result.ok();
	}

	//api/album/albumInfo/getAlbumInfo/1594
	@GuiguLogin
	@Operation(summary = "根据id回显查询专辑信息")
	@GetMapping("getAlbumInfo/{id}")
	public Result<AlbumInfo> getAlbumInfo(@PathVariable("id") Long id){
		 AlbumInfo albumInfo =albumInfoService.getAlbumInfo(id);
		 return Result.ok(albumInfo);
	}

	//api/album/albumInfo/updateAlbumInfo/1594
	@GuiguLogin
	@Operation(summary = "修改专辑信息") //相当于把之前的删除后又重新新增保存
	@PutMapping("updateAlbumInfo/{id}")
	public Result updateAlbumInfo(@PathVariable("id") Long id,
								  @RequestBody AlbumInfoVo albumInfoVo) {
		albumInfoService.updateAlbumInfo(id, albumInfoVo);
		return Result.ok();
	}
//----------------------------------声音--------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
	//api/album/albumInfo/findUserAllAlbumList
    @GuiguLogin
	@Operation(summary = "获取当前用户全部专辑列表")
	@GetMapping("findUserAllAlbumList")
	public Result findUserAllAlbumList(){
		List<AlbumInfo> list = albumInfoService.findUserAllAlbumList(AuthContextHolder.getUserId());

		return Result.ok(list);
	}

	//--------------------------------es搜索---------------------------------------------------------------
	//-----------------------------------------------------------------------------------------------------
	@Operation(summary = "内部调用:es导入数据时查询专辑分页数据")
	@GetMapping("getAlbumListVoPage/{pageNum}/{pageSize}")
	public Result<Page<AlbumListVo>> getAlbumListVoPage(@PathVariable Long pageNum,
									   @PathVariable Long pageSize){
		Page<AlbumListVo> page = albumInfoService.getAlbumListVoPage(pageNum,pageSize);
		return Result.ok(page);
	}

	//根据专辑id查询专辑的属性和值列表  AttributeValueIndex
	@Operation(summary = "内部接口：根据专辑id查询专辑的属性和值id列表")
	@GetMapping("findAttributesByAlbumId/{albumId}")
	public Result<List<AttributeValueIndex>> findAttributesByAlbumId(
			@PathVariable("albumId")Long albumId){
		List<AttributeValueIndex> attributeValueIndexList = albumInfoService
				.findAttributesByAlbumId(albumId);
		return Result.ok(attributeValueIndexList);
	}

	@Operation(summary = "内部调用:es导入数据时根据专辑id查询专辑详情")
	@GetMapping("getAlbumListVo/{id}")
	public Result<AlbumListVo> getAlbumListVo(
			@PathVariable("id") Long id
	){
		AlbumListVo albumListVo = albumInfoService
				.getAlbumListVo(id);
		return Result.ok(albumListVo);
	}



	@Operation(summary = "内部接口：根据id查询专辑数据")
	@GetMapping("findAlbumInfo/{id}")
	public Result<AlbumInfo> findAlbumInfo(@PathVariable("id") Long id) {
		AlbumInfo albumInfo = albumInfoService.getAlbumInfo(id);
		return Result.ok(albumInfo);
	}

	@Operation(summary = "内部调用:根据专辑id查询统计数据")
	@GetMapping("getAlbumStatVo/{id}")
	public Result<AlbumStatVo> getAlbumStatVo(
			@PathVariable("id") Long id
	){
		AlbumStatVo albumStatVo = albumInfoService
				.getAlbumStatVo(id);
		return Result.ok(albumStatVo);
	}

}

